package cn.maihe.elg.operation.centers.epoint.channel.service;

import cn.maihe.elg.operation.centers.epoint.channel.EpointBaseChannelService;
import cn.maihe.elg.operation.centers.epoint.dto.EpointBaseRespDTO;
import cn.maihe.elg.operation.centers.epoint.dto.channel.EpointBaohanFileCompleteNoticeReqDTO;
import cn.maihe.elg.operation.centers.epoint.enums.EpointChannelMethod;
import cn.maihe.elg.operation.centers.epoint.enums.EpointRespCodeEnum;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import cn.maihe.elg.operation.utils.ValidateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description
 * 金融服务系统作为服务方，金融机构作为请求方。
 * 保函文件上传成功后，金融机构主动推送解密秘钥告知金融服务系统进行解密。
 * @Author WJH
 * @Date 2021/04/21
 */
@Service
@Slf4j
public class NPEpointBaohanFileCompleteNoticeChannelService extends EpointBaseChannelService<EpointBaohanFileCompleteNoticeReqDTO, EpointBaseRespDTO> {

    @Resource
    private ElgResourceService elgResourceService;

    @Override
    public EpointChannelMethod methodEnum() {
        return EpointChannelMethod.baohanfilecompletenotice;
    }


    public InnerBaseResp baohanFileCompleteNotice(String acceptOrderNo, CenterNoEnum centerNoEnum, EpointBaohanFileCompleteNoticeReqDTO reqDTO) {
        InnerBaseResp<String> innerBaseResp = null;

        try {
            ValidateUtil.validate(reqDTO);
        } catch (Exception e) {
            innerBaseResp = InnerBaseResp.error("保函文件上传完成通知参数有误:" + e.getMessage());
            return innerBaseResp;
        }
        try {
            EpointBaseRespDTO resp = this.doRequest( centerNoEnum, reqDTO);
            if (EpointRespCodeEnum.isSuccess(resp.getCode())) {
                innerBaseResp = InnerBaseResp.success("保函文件上传完成通知成功");
            } else {
                innerBaseResp = InnerBaseResp.error("通知返回失败："+resp.getMessage());
            }
        } catch (Exception e) {
            log.error("保函文件上传完成通知 >> {}: [{}] 异常:{}", acceptOrderNo,centerNoEnum.getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("保函文件上传完成通知异常:" + e.getMessage());
        }
        return innerBaseResp;
    }
}
